package com.dpad.telematicsclientapp.service;

import android.app.Service;
import android.content.Intent;
import android.os.IBinder;

import com.amap.api.location.AMapLocation;
import com.amap.api.location.AMapLocationClient;
import com.amap.api.location.AMapLocationClientOption;
import com.amap.api.location.AMapLocationListener;
import com.dpad.telematicsclientapp.netlibrary.AppConstants;
import com.dpad.telematicsclientapp.netlibrary.MainApplicaton;
import com.dpad.telematicsclientapp.netlibrary.http.EncryptUtils.SecretResult;
import com.dpad.telematicsclientapp.netlibrary.http.NetInstance;
import com.dpad.telematicsclientapp.util.utils.ParameterUtils;

import java.util.SortedMap;
import java.util.TreeMap;

import io.reactivex.Observer;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.Disposable;
import io.reactivex.schedulers.Schedulers;

public class GetLocalInfoService extends Service {
    private double latitude;
    private double longitude;
    private static final String TAG = GetLocalInfoService.class.getSimpleName();
    public static final String ACTION = "com.vigoss.service.PollingService";
    //声明AMapLocationClient类对象
    public AMapLocationClient mLocationClient = null;
    public AMapLocationListener mLocationListener = new AMapLocationListener() {
        @Override
        public void onLocationChanged(AMapLocation amapLocation) {
            if (amapLocation != null) {
                if(amapLocation.getLatitude()!=latitude&&amapLocation.getLongitude()!=longitude){

                    latitude = amapLocation.getLatitude();
                    longitude = amapLocation.getLongitude();
                }
            }
        }
    };
    //声明AMapLocationClientOption对象
    public AMapLocationClientOption mLocationOption = null;
    @Override
    public IBinder onBind(Intent intent) {
        return null;
    }
    @Override
    public void onCreate() {
        mLocationClient = new AMapLocationClient(getApplicationContext());
        mLocationClient.setLocationListener(mLocationListener);
        mLocationOption = new AMapLocationClientOption();
        AMapLocationClientOption option = new AMapLocationClientOption();
        mLocationOption.setLocationMode(AMapLocationClientOption.AMapLocationMode.Hight_Accuracy);
        mLocationClient.setLocationOption(mLocationOption);
    }

    @Override
    public void onStart(Intent intent, int startId) {
        new PollingThread().start();
    }

    /**
     * Polling thread
     * 模拟向Server轮询的异步线程
     */
    int count = 0;
    class PollingThread extends Thread {
        @Override
        public void run() {
            mLocationClient.startLocation();
            if(MainApplicaton.LOGINRESULTVO.getToken().equals("")|| MainApplicaton.LOGINRESULTVO.getToken()==null){
                return;
            }
            SortedMap<String, Object> sortedMap = new TreeMap();
            sortedMap.put("lng", longitude);
            sortedMap.put("lat", latitude);
            sortedMap.put("diu",  AppConstants.getMyUUID());
            NetInstance.getEventsService().getGdStatus(ParameterUtils.getHeaser(sortedMap), ParameterUtils.getJsonBody(sortedMap)).
                    subscribeOn(Schedulers.io()).
                    observeOn(AndroidSchedulers.mainThread()).
                    subscribe(new Observer<SecretResult>() {
                        @Override
                        public void onSubscribe(Disposable d) {
                        }

                        @Override
                        public void onNext(SecretResult entity) {
                        }

                        @Override
                        public void onError(Throwable e) {
                        }

                        @Override
                        public void onComplete() {
                        }
                    });
        }
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        System.out.println("Service:onDestroy");
    }
}
